/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved.
 * <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.codec.custom;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

import com.cqcis.uip.base.framework.codec.DataFormatException;
import com.cqcis.uip.base.framework.codec.ObjectDecoder;
import com.cqcis.uip.base.framework.utils.ByteBuffer;

/**
 * 解包格式参见, PF-中地  接口规范
 * @author 李世军 创建于 上午11:56:38
 */
public class ZyObjectDecoderImpl implements ObjectDecoder {
	private static Log log = LogFactory.getLog(ZyObjectDecoderImpl.class);
	private int version = 0x02 ;//版本号，当前为0x02
	
	public Object decode(ByteBuffer buffer) throws Exception {
		// 包头 14个字节
		if (!buffer.hasRemaining() || buffer.remaining() < 14) {
			return null;
		}
		
		int textLen = 0 ; // 正文长度
		
		byte srcSystem[] = new byte[2] ;// 源系统
		byte targetSystem[] = new byte[2] ;//目标系统
		byte text[] ;
		
		// ---- 获取正文 ----
		if(buffer.get()==version){
			buffer.getInt() ; 			//消息头的长度
			textLen = buffer.getInt() ; //正文长度
			buffer.get() ;				//服务类型：1为响应，2为请求
			buffer.get(srcSystem) ;     //源系统
			buffer.get(targetSystem) ;  //目标系统
			text = new byte[textLen] ;
			buffer.get(text) ; 			// 正文
		}else{
			throw new DataFormatException("Socket packet head verify Exception: Please Check File. ");
		}
		
		
		// ---- 解析正文 ----
//		parseXml(new String(text,Charset.forName("gb2312"))) ;
		
		return new String(text) ;
	}
	
	public static void parseXml(String xml){
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml);
		} catch (DocumentException e) {
			throw new RuntimeException("解析XML错误:"+e.getMessage()) ;
		}

		Element root = doc.getRootElement();// 指向根节点  -- REQUEST_XML
		log.info("--解析正文内容--");
		log.info(root.element("DOMAIN_SEND").getTextTrim());
		log.info(root.element("DOMAIN_RECEIVE").getTextTrim());
		log.info(root.element("OPERATOR").getTextTrim());
		log.info(root.element("TIMES").getTextTrim());
		log.info(root.element("REGION").getTextTrim());
		log.info(root.element("OPERATION_NAME").getTextTrim());
		
		Element requestInfo = root.element("REQUEST_INFORMATION");
		log.info(requestInfo.element("ORDERTICKET_ID").getTextTrim());
		log.info(requestInfo.element("TASKITEM_ID").getTextTrim());
		log.info(requestInfo.element("SERIAL_NO").getTextTrim());
		log.info(requestInfo.element("RESULT").getTextTrim());
		log.info(requestInfo.element("CIRCUIT_NO").getTextTrim());
		
	}
	
	public static void main(String[] args) {
//		String xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" 
//					+" <REQUEST_XML> "
//					+" 	<DOMAIN_SEND>NO</DOMAIN_SEND> "
//					+" 	<DOMAIN_RECEIVE>SF</DOMAIN_RECEIVE> "
//					+" 	<OPERATOR>huhongzhi</OPERATOR> "
//					+" 	<TIMES>2010-5-5 8:45:57</TIMES> "
//					+" 	<REGION>230</REGION> "
//					+" 	<OPERATION_NAME>REPORT_CONFIG_RESULT_REQUEST</OPERATION_NAME> "
//					+" 	<REQUEST_INFORMATION> "
//					+" 		<ORDERTICKET_ID></ORDERTICKET_ID> "
//					+" 		<TASKITEM_ID>order_1273020293007q5zrnp-127302029300704715SF_31649205</TASKITEM_ID> "
//					+" 		<SERIAL_NO>SF_31649205</SERIAL_NO> "
//					+" 		<RESULT>SUCCESS</RESULT> "
//					+" 		<CIRCUIT_NO>F1005050001</CIRCUIT_NO> "
//					+" 	</REQUEST_INFORMATION> "
//					+" </REQUEST_XML> " ;
		

		String xml = "<?xml version=\"1.0\" encoding=\"GB2312\"?>" 
			+" <REQUEST_XML> "
			+" 	<DOMAIN_SEND/> "
			+" 	<DOMAIN_RECEIVE>SF</DOMAIN_RECEIVE> "
			+" 	<OPERATOR>huhongzhi</OPERATOR> "
			+" 	<TIMES>2010-5-5 8:45:57</TIMES> "
			+" 	<REGION>230</REGION> "
			+" 	<OPERATION_NAME>REPORT_CONFIG_RESULT_REQUEST</OPERATION_NAME> "
			+" 	<REQUEST_INFORMATION/> "
			+" </REQUEST_XML> " ;

		
		
		Document doc = null;
		try {
			doc = DocumentHelper.parseText(xml);
		} catch (DocumentException e) {
			throw new RuntimeException("解析XML错误:"+e.getMessage()) ;
		}

		Element root = doc.getRootElement();// 指向根节点  -- REQUEST_XML
		System.out.println("--解析正文内容--");
		System.out.println(root.element("DOMAIN_SEND").getTextTrim());
		System.out.println(root.element("DOMAIN_RECEIVE").getTextTrim());
		System.out.println(root.element("OPERATOR").getTextTrim());
		System.out.println(root.element("TIMES").getTextTrim());
		System.out.println(root.element("REGION").getTextTrim());
		System.out.println(root.element("OPERATION_NAME").getTextTrim());
		
		Element requestInfo = root.element("REQUEST_INFORMATION");
		if(requestInfo.hasContent()){
			System.out.println(requestInfo.element("ORDERTICKET_ID").getTextTrim());
			System.out.println(requestInfo.element("TASKITEM_ID").getTextTrim());
			System.out.println(requestInfo.element("SERIAL_NO").getTextTrim());
			System.out.println(requestInfo.element("RESULT").getTextTrim());
			System.out.println(requestInfo.element("CIRCUIT_NO").getTextTrim());
		}
	}
	
}
